from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from xigua_selenium import biz_mysql


def xigua_selenium(Novel_name, insert_time, Chapter, Extension_name, Platform):
    driver = webdriver.Chrome()
    # 进入 西瓜
    driver.get("https://www.koread.cn/admin/notice?ref=addtabs")
    driver.maximize_window()
    driver.implicitly_wait(20)
    if "雨季" in Platform:
        username = ""
    else:
        username = ""

    # 定位账号
    driver.find_element_by_name('username').clear()
    driver.find_element_by_name('username').send_keys(username)

    # 定位密码
    driver.find_element_by_name('password').clear()
    driver.find_element_by_name('password').send_keys(password)

    # 点击登录
    driver.find_element_by_xpath('//div[5]/button').click()

    # 点击小说推广
    try:
        time.sleep(1)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//section/ul/li[4]/a'))
        )
        driver.find_element_by_xpath('//section/ul/li[4]/a').click()
    except Exception:
        print("点击小说推广error")

    # 点击小说中心
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//li[4]//li[1]/a'))
        )
        driver.find_element_by_xpath('//li[4]//li[1]/a').click()
    except Exception:
        print("点击小说中心error")

    # 点击名称选择搜索框
    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="name"]'))
        )
        driver.find_element_by_xpath('//*[@id="name"]').clear()
        # time.sleep(1)
        driver.find_element_by_xpath('//*[@id="name"]').send_keys(Novel_name)
    except Exception:
        print("点击名称选择搜索框error")

    # 点击提交
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//div[9]//button[1]'))
        )
        driver.find_element_by_xpath('//div[9]//button[1]').click()
    except Exception:
        print("点击提交error")

    # 点击推广连接
    try:
        time.sleep(1)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//td[8]/a'))
        )
        driver.find_element_by_xpath('//td[8]/a').click()
    except Exception:
        print("点击推广连接error")

    # 新iframe

    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe{0}"]'.format(1)))

    # 点击推广链接
    try:

        # WebDriverWait(driver, 15).until(
        #     EC.element_to_be_clickable((By.XPATH, '//div[9]//button[1]'))
        # )
        driver.find_element_by_xpath('//tr[{0}]//a[4]'.format(Chapter)).click()
    except Exception:
        print('点击推广链接error')

    # 新Iframe

    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="c-name"]'))
        )
        driver.find_element_by_xpath('//*[@id="c-name"]').clear()
        driver.find_element_by_xpath('//*[@id="c-name"]').send_keys('{0}'.format(str(insert_time) + Extension_name))
    except Exception:
        print("输入渠道名称error")

    driver.find_element_by_xpath('//*[@id="c-guide_chapter_idx"]').send_keys(3)

    # 确认
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//button[text()="确定"]'))
        )
        driver.find_element_by_xpath('//button[text()="确定"]').click()
        time.sleep(2)
        url = driver.find_element_by_xpath('//*[@id="add-dialog"]//div[1]/div[2]/div[2]').text
        print(url)
        # 数据入库
        biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                     datetime=insert_time, Extension_name=Extension_name)
    except Exception:
        print("确认出错")
    # 退出当前iframe
    driver.switch_to.default_content()

    # 取消
    try:

        driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))

        driver.find_element_by_xpath('//*[@id="layui-layer{0}"]/span[1]/a[3]'.format(1)).click()
    except Exception:
        print("取消1")

    driver.switch_to.default_content()

    driver.quit()
